<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="gae.google.tcserver.db.*"%>
<%@ page import="gae.google.tcserver.model.*"%>
<%@ page import="gae.google.tcserver.api.*"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" href="../CSS/TeamCalendar.css" rel="stylesheet" />
<title>Team Members</title>

</head>
<body class="body">
<%

	// get team id from URL
	String teamId = request.getParameter("id");
	Key teamKeyId = KeyFactory.stringToKey(teamId);
	Team team = GetFromDB.getTeam(teamKeyId);
	Key teamCreatorKeyId = team.getUserKeyId();
	
	HttpSession s = request.getSession(false);
	Key userKeyId = (Key)s.getAttribute("userkeyid");
	Boolean admin = false;
	Boolean teamCreator = false;
	if (userKeyId != null){
		try {
			if (userKeyId.getId() == teamCreatorKeyId.getId()){
				teamCreator = true;
			}
		}catch (Exception e) {
	    }
	}

	// holds a list of team members
	List<TeamMemberBean> teamMembers = new ArrayList<TeamMemberBean>();

	String teamMemberKeyId = null;
	long noOfTeamMembers = 0;

	// get json

	CreateJson createJson = new CreateJson();
	String json = createJson.getTeamMembers(team.getKeyId());
	
	// did we get a json
	if (json != null) {
		// yes, parse json to get all members
		ParseJson jsonParser = new ParseJson();
		teamMembers =  jsonParser.parseTeamMembers(json);
		if (teamMembers != null) {
			noOfTeamMembers = teamMembers.size();
		}
	}
%>
	<div class="mainContainer">
    	
        <div class="topContainer">
        	<img src="../PICS/TeamCalendar.jpg" style="height: 112px; width: 1100px"/> 
        </div>
        
        <div class="mainMenuContainer">
	        <%String urlHome = "/views/TeamCalendar.jsp";%>
	        <%if (teamCreator){%>
	        	<a href=<%=urlHome%>>Home</a>
	        	<a href="javascript:void document.form.submit();">Calendar</a>
	        <%}%>
        </div>
        
 		<%String image = null;%>
 		<%if (teamMembers.size() != 0){%>
 	 		<%for (TeamMemberBean teamMember : teamMembers) {%>
 	 			<%image = teamMember.getImageName();%>
 				<%if (image == null || image.contains("Image Name") || image.length() == 0){%>
 					<%image = "../PICS/NoImageSelected1.jpg";%>
				<%}else{%>
					<%image = "../PICS/" + teamMember.getImageName();%>
				<%}%>
	        	<%String urlEdit = "/views/MemberEdit.jsp";%>
	        	<%String urlRemove = "/views/MemberRemove.jsp";%>
	        	<div class="members">
					<img src=<%=image%> style="border:1px solid LightGrey; height: 150px; width: 100px"></img><br>
					<a href=<%=urlEdit%>>Edit</a>
					<a href=<%=urlRemove%>>Remove</a>
				</div>
				<div class="memberInfo">
					<p><%=teamMember.getRole()%></p>
					<p><%=teamMember.getFirstName()%> <%=teamMember.getFamilyName()%></p>
					<p><%=teamMember.getAddress()%></p>
					<p><%=teamMember.getZipCode()%> <%=teamMember.getLocation()%></p>
					<p>Mobil:  <%=teamMember.getPhoneNumber()%></p>
					<p>Mail:   <%=teamMember.getMailAddress()%></p>
				</div>
			<%}%>
		<%}else{%>
	        <div class="messageContainer">
	        	<p class="light">There are no members added to the team.</p>
	        </div>
		<%}%>
		<form name="form" method="post" action="/Calendar.do">
			<input type="hidden" name="year" value=""/>
			<input type="hidden" name="month" value=""/>
		</form>
	</div>
</body>
</html>